package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelContentDto;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * @author WeitenHong
 * @date 2020/10/16 11:03
 */
@Service
public class HomeServiceImpl implements IHomeService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    @Override
    public HomePageResponse homepage() {
        HomePageResponse homePageResponse = new HomePageResponse();
        ArrayList<PanelDto> panelDtos = new ArrayList<>();
        List<Panel> panels = panelMapper.selectAll();
        for (Panel p : panels) {
            Integer panelId = p.getId();
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panelId);
            p.setPanelContentItems(panelContentItems);
            PanelDto panelDto = contentConverter.panen2Dto(p);
            panelDtos.add(panelDto);
        }
        homePageResponse.setPanelContentItemDtos(panelDtos);
        return homePageResponse;
    }

    @Override
    public List<PanelContentDto> getNavigation() {
        PanelContent panelContent = new PanelContent();
        panelContent.setPanelId(0);
        List<PanelContent> panelContents = panelContentMapper.select(panelContent);
        List<PanelContentDto> panelContentDtos = contentConverter.panelContents2Dto(panelContents);
        return panelContentDtos;
    }
}
